import type { AsyncBasicHttpSendData, BasicFetchResult, BasicHttpSendData } from '@/api/model/baseModel';
import type {
  LoginResultModel,
  UpdateLoginDto,
  UpdateRegisterDto,
  UserInfoResultModel,
} from '@/api/model/index';
import { ContentTypeEnum } from '@/enums/httpEnum';
import { defHttp } from '@/utils/http/axios';

export async function login(data: UpdateLoginDto) {
  return defHttp.post<LoginResultModel>(
    {
      url: '/login',
      data,
    },
    {
      successMessageMode: 'message',
      errorMessageMode: 'message',
      withToken: false,
    },
  );
}

export async function register(data: UpdateRegisterDto) {
  return defHttp.post<BasicFetchResult<any>>(
    {
      url: '/register',
      data,
    },
    {
      // successMessageMode: 'message',
      errorMessageMode: 'message',
      withToken: false,
    },
  );
}

export async function getInfo() {
  return defHttp.get<UserInfoResultModel>({
    url: '/getInfo',
  });
}

export async function logout() {
  return defHttp.post<BasicHttpSendData>({
    url: '/logout',
  });
}

export async function getCodeImg(): Promise<BasicHttpSendData<any>> {
  return new Promise((resolve, reject) => {
    defHttp
      .get<BasicHttpSendData<any>>({
        url: '/captchaImage',
      })
      .then((res: any) => {
        res.data = {
          img: res?.data?.img ?? res.img,
          ...(res?.data ?? {}),
          ...res,
        };
        return resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

// 忘记密码
export async function forgetPassword(data) {
  return defHttp.post<BasicHttpSendData>(
    {
      url: '/sms/updateForgetPwd',
      data,
      headers: {
        'Content-type': ContentTypeEnum.FORM_URLENCODED,
        // @ts-ignore
      },
    },
    {
      successMessageMode: 'message',
      errorMessageMode: 'message',
      ignoreCancelToken: false,
    },
  );
}

// 获取验证码
export async function getTelCodeApi(phonenumber: any) {
  return defHttp.get<BasicHttpSendData>(
    {
      url: '/sms/sendForgetPwdCode',
      params: {
        phonenumber,
      },
    },
    {
      // successMessageMode: 'message',
      errorMessageMode: 'message',
    },
  );
}

// 获取验证码
export async function getTelRegisterCodeApi(phonenumber: any) {
  return defHttp.get<BasicHttpSendData>(
    {
      url: '/sms/sendRegisterCode',
      params: { phonenumber },
    },
    {
      // successMessageMode: 'message',
      errorMessageMode: 'message',
    },
  );
}
// Compare this snippet from src/api/permission.ts:

// 用户修改密码
export async function updatePwd(data: any) {
  return defHttp.put<BasicHttpSendData>(
    {
      url: '/system/user/profile/updatePwd',
      data,
    },
    {
      successMessageMode: 'message',
      errorMessageMode: 'message',
    },
  );
}

export async function studyBooksStateModal(
  params?: Record<string, any>,
): Promise<AsyncBasicHttpSendData<Record<string, any>>> {
  return defHttp.get({
    url: '/order/login/study',
    params,
  });
}

export async function getQRCode() {
  return defHttp.get({
    url: '/wechat/getQRCode',
  });
}

export async function wechatAuth(code) {
  return defHttp.get(
    {
      url: '/wechat/auth/callback',
      params: {
        code,
      },
    },
    {
      errorMessageMode: 'none',
    },
  );
}
